/*
 * @Author: szx
 * @Date: 2022-02-16 13:39:45
 * @LastEditTime: 2022-02-16 13:51:24
 * @Description:
 * @FilePath: \leetcode\1-99\69\69.js
 */
/**
 * @param {number} x
 * @return {number}
 */
var mySqrt = function (x) {
    let left = 0,
        right = x;
    let res = -1;
    while (left <= right) {
        const mid = Math.floor((left + right) / 2);
        if (mid * mid <= x) {
            res = mid;
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return res;
};

// 牛顿迭代法
var mySqrt = function (x) {
    if (x == 0) return 0;
    let C = x,
        x0 = x;
    while (true) {
        let xi = 0.5(x0 + Math.floor(C / x0));
        if (Math.abs(xi - x0) < 1e-7) break;
        x0 = xi;
    }
    return x0;
};
